package org.martin.snsrs.daos;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.FetchMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.martin.snsrs.beans.Tags;
import org.martin.snsrs.beans.User;
import org.martin.snsrs.beans.UserSession;
import org.martin.snsrs.utils.MathUtil;
import org.springframework.orm.hibernate3.HibernateTemplate;

//user相关数据库操作
public class UserDao {
	
	private HibernateTemplate hibernateTemplate;
	
	private SessionFactory sessionFactory;
	
	private Session session;
	
	public void setSessionFactory(SessionFactory sessionFactory){
		hibernateTemplate = new HibernateTemplate(sessionFactory);
		this.sessionFactory = sessionFactory;
		session = sessionFactory.openSession();
	}
	
	//保存User
	public void saveUser(User user){
		hibernateTemplate.save(user);
		hibernateTemplate.flush();
	}
	
	public User getUser(Integer userID){
		return hibernateTemplate.get(User.class, userID);
	}
	
	public User getUserByUsername(String name){
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.eq("username", name));
		@SuppressWarnings("unchecked")
		List<User> users = (List<User>) criteria.list();
		if(users != null && users.size() > 0)
			return users.get(0);
		return null;
	}
	
	public User getUserByNickname(String nickname){
		Query query = session.createQuery("From User WHERE nickname='" + nickname +"'");
		@SuppressWarnings("unchecked")
		List<User> users = hibernateTemplate.find(query.getQueryString());
		if(users != null)
			return users.get(0);
		return null;
	}
	
	public List<User> searchUsers(String nickname, String username){
		Criteria criteria = session.createCriteria(User.class);
		criteria.add(Restrictions.like("nickname", nickname));
		criteria.add(Restrictions.like("username", username));
		@SuppressWarnings("unchecked")
		List<User> users = criteria.list();
		if(session != null)
			session.close();
		return users;
	}

	
	public boolean login(String username, String password){
		try{
			String pass = MathUtil.getMD5(password);
			Criteria criteria = session.createCriteria(User.class);
			criteria.add(Restrictions.eq("username", username));
			@SuppressWarnings("unchecked")
			List<User> list = criteria.list();
			if(list.get(0).getUserPass().equals(pass))
				return true;
			else {
				return false;
			}
		}catch(Exception ex){
			ex.printStackTrace();
			return false;
		}
	}
	
	public List<String> getUserSession(String username){
		try{
			Criteria criteria = session.createCriteria(UserSession.class);
			criteria.add(Restrictions.eq("username", username));
			criteria.setFetchMode("user", FetchMode.JOIN);
			@SuppressWarnings("unchecked")
			List<UserSession> sessions = criteria.list();
			ArrayList<String> sessionKeys = new ArrayList<String>();
			for(UserSession userSession : sessions){
				sessionKeys.add(userSession.getSessionKey());
			}
			return sessionKeys;
		}
		catch(Exception ex){
			return null;
		}
	}
	
	public void saveUserSession(String username, String SessionKey){
		UserSession session = new UserSession();
		session.setSessionKey(SessionKey);
		session.setUser(getUserByUsername(username));
		hibernateTemplate.save(session);
		hibernateTemplate.flush();
	}
	
	public User getUserBySessionKey(String sessionKey){
		Criteria criteria = session.createCriteria(UserSession.class);
		criteria.add(Restrictions.eq("sessionKey", sessionKey));
		criteria.setFetchMode("user", FetchMode.JOIN);
		@SuppressWarnings("unchecked")
		List<UserSession> sessions = criteria.list();
		if(sessions.size() > 0)
			return sessions.get(0).getUser();
		else {
			return null;
		}
	}
}
